package rexsee.core.widget;

import rexsee.core.style.RexseeDrawable;
import rexsee.core.utilities.Utilities;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils.TruncateAt;
import android.text.method.ScrollingMovementMethod;
import android.text.util.Linkify;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LabelDiv extends Div {

	public final ImageView mIcon;
	public final TextView mLabel;
	private final LinearLayout mContainer;

	public LabelDiv(Context context) {

		super(context);
		mIcon = new ImageView(context);
		mLabel = new TextView(context);
		mContainer = new LinearLayout(context);
		this.addDivChild(mContainer);

	}

	protected final void setLabelStyle() {

		mContainer.setGravity(styleSheet.getGravity());
		mContainer.removeAllViews();

		setDivStyle();

		try {
			if (styleSheet.icon_visibility.equalsIgnoreCase("visible") && styleSheet.label_visibility.equalsIgnoreCase("visible")) {
				setTextViewStyle();
				setImageViewStyle();
				if (styleSheet.icon_position.equalsIgnoreCase("left")) {
					mContainer.setOrientation(LinearLayout.HORIZONTAL);
					mContainer.addView(mIcon);
					mContainer.addView(mLabel);
				} else if (styleSheet.icon_position.equalsIgnoreCase("right")) {
					mContainer.setOrientation(LinearLayout.HORIZONTAL);
					mContainer.addView(mLabel);
					mContainer.addView(mIcon);
				} else if (styleSheet.icon_position.equalsIgnoreCase("bottom")) {
					mContainer.setOrientation(LinearLayout.VERTICAL);
					mContainer.addView(mLabel);
					mContainer.addView(mIcon);
				} else {
					mContainer.setOrientation(LinearLayout.VERTICAL);
					mContainer.addView(mIcon);
					mContainer.addView(mLabel);
				}
			} else if (styleSheet.icon_visibility.equalsIgnoreCase("visible")) {
				setImageViewStyle();
				mContainer.addView(mIcon);
			} else if (styleSheet.label_visibility.equalsIgnoreCase("visible")) {
				setTextViewStyle();
				mContainer.addView(mLabel);
			} else {
			}
		} catch (Exception e) {
			Toast.makeText(mContext, "LabelDiv1:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}

	}

	private void setImageViewStyle() {
		try {
			if (styleSheet.icon_visibility.equalsIgnoreCase("visible")) {
				mIcon.setLayoutParams(styleSheet.getIconLayoutParams());
				mIcon.setPadding(styleSheet.getIconPaddingLeft(), styleSheet.getIconPaddingTop(), styleSheet.getIconPaddingRight(), styleSheet.getIconPaddingBottom());
				mIcon.setAlpha(styleSheet.getIconAlpha());
				mIcon.setBackgroundDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.icon_background_color));
				mIcon.setScaleType(styleSheet.getIconScaleType());
				mIcon.setImageDrawable(styleSheet.icon);
			}
		} catch (Exception e) {
			Toast.makeText(mContext, "LabelDiv2:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
	}
	private void setTextViewStyle() {
		try {
			if (styleSheet.label_visibility.equalsIgnoreCase("visible")) {
				mLabel.setLayoutParams(styleSheet.getLabelLayoutParams());
				mLabel.setBackgroundDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.label_background_color));
				mLabel.setPadding(styleSheet.getLabelPaddingLeft(), styleSheet.getLabelPaddingTop(), styleSheet.getLabelPaddingRight(), styleSheet.getLabelPaddingBottom());
				mLabel.setTextSize(Float.parseFloat(styleSheet.font_size.toLowerCase().replaceAll("px", "")));
				mLabel.setTextColor(Color.parseColor(styleSheet.color));
				mLabel.setTypeface(null, styleSheet.getTypefaceStyle());
				mLabel.setGravity(styleSheet.getTextGravity());
				mLabel.setShadowLayer(
						Float.parseFloat(styleSheet.text_shadow_radius),
						Float.parseFloat(styleSheet.text_shadow_dx.toLowerCase().replaceAll("px", "")),
						Float.parseFloat(styleSheet.text_shadow_dy.toLowerCase().replaceAll("px", "")),
						Color.parseColor(styleSheet.text_shadow_color));
				mLabel.setAutoLinkMask((styleSheet.text_link.equalsIgnoreCase("autolink")) ? Linkify.ALL : 0);
				if (styleSheet.text_overflow.equalsIgnoreCase("marquee")) {
					mLabel.setFocusable(true);
					mLabel.setHorizontallyScrolling(true);
					mLabel.setEllipsize(TruncateAt.MARQUEE);
					mLabel.setMarqueeRepeatLimit(-1);
					mLabel.setMovementMethod(ScrollingMovementMethod.getInstance());
				} else if (styleSheet.text_overflow.equalsIgnoreCase("start")) {
					mLabel.setHorizontallyScrolling(false);
					mLabel.setEllipsize(TruncateAt.START);
				} else if (styleSheet.text_overflow.equalsIgnoreCase("end")) {
					mLabel.setHorizontallyScrolling(false);
					mLabel.setEllipsize(TruncateAt.END);
				} else if (styleSheet.text_overflow.equalsIgnoreCase("middle")) {
					mLabel.setHorizontallyScrolling(false);
					mLabel.setEllipsize(TruncateAt.MIDDLE);
				} else {
					mLabel.setHorizontallyScrolling(false);
				}
				boolean singleLine = (styleSheet.white_space.equalsIgnoreCase("nowrap")) ? true : false;
				mLabel.setSingleLine(singleLine);
				if (!singleLine) {
					float mult = Utilities.getInt(styleSheet.line_height.toLowerCase().replaceAll("%", ""), 100) / 100f;
					mLabel.setLineSpacing(0, mult);
				}
				if (styleSheet.label_type.equalsIgnoreCase("html")) {
					try {
						mLabel.setText(Html.fromHtml(styleSheet.label));
					} catch (Exception e) {
						mLabel.setText(Html.fromHtml(styleSheet.label).toString());
					}
				} else if (styleSheet.label_type.equalsIgnoreCase("string")) {
					mLabel.setText(Html.fromHtml(styleSheet.label).toString());
				} else {
					mLabel.setText(styleSheet.label);
				}
			}
		} catch (Exception e) {
			Toast.makeText(mContext, "LabelDiv3:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
	}

	protected final void setLabelStyle(String style) {
		styleSheet.parseStyle(style);
		setLabelStyle();
	}
	protected final String getLabelStyle(String name) {
		if (name.equalsIgnoreCase("icon-width")) {
			return String.valueOf(mIcon.getWidth());
		} else if (name.equalsIgnoreCase("icon-height")) {
			return String.valueOf(mIcon.getHeight());
		} else if (name.equalsIgnoreCase("label-width")) {
			return String.valueOf(mLabel.getWidth());
		} else if (name.equalsIgnoreCase("label-height")) {
			return String.valueOf(mLabel.getHeight());
		} else {
			return getDivStyle(name);
		}
	}
	@Override
	public void setStyle(String style) {
		setLabelStyle(style);
	}
	@Override
	public String getStyle(String name) {
		return getLabelStyle(name);
	}

}
